package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.bean.CartInfo;
import com.atguigu.gmall.cart.interceptor.LoginInterceptor;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.bean.ResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/")
public class CartController {

    @Resource
    CartService cartService;

    @ResponseBody
    @ApiOperation("根据userId查询选中的购物项列表")
    @GetMapping("user/{userId}")
    public ResponseVo<List<CartInfo>> queryCheckedCartInfos(@PathVariable("userId")Long userId){
        return ResponseVo.ok(cartService.queryCheckedCartInfosByUserId(userId));
    }


    @ApiOperation("删除购物项")
    @ResponseBody
    @PostMapping("/batchDelCartInfos")
    public ResponseVo batchDelCartInfos()
    {
        cartService.batchDelCartInfos();
        return ResponseVo.ok();
    }

    // 服务器地址：访问服务器(ip地址找服务器电脑、端口号找服务器应用)
    // 资源路径：  找服务器应用中对应的资源
    @ApiOperation("删除购物项")
    @ResponseBody
    @PostMapping("/deleteCart")
    public ResponseVo deleteCartInfo(@RequestParam(value = "skuId") String skuId){
        cartService.deleteCartInfoBySkuId(skuId);
        return ResponseVo.ok();
    }
    @ApiOperation("更新购物项数量")
    @ResponseBody
    @PostMapping("/updateNum")
    public ResponseVo updateNum(@RequestBody CartInfo cartInfo){
        cartService.updateNum(cartInfo);
        return ResponseVo.ok();
    }
    @ApiOperation("更新购物项选中状态")
    @ResponseBody
    @PostMapping("/updateStatus")
    public ResponseVo updateStatus(@RequestBody CartInfo cartInfo){
        cartService.updateStatus(cartInfo);
        return ResponseVo.ok();
    }
    @ApiOperation("展示购物车数据")
    @GetMapping("/cart.html")
    public String cartHtml(Model model){

        List<CartInfo> carts = cartService.queryCartInfos();
        model.addAttribute("carts",carts);
        return "cart";
    }


//    @ResponseBody
    @ApiOperation("添加skuId的商品到购物车")
    @GetMapping
    public String add2Cart(@RequestParam("skuId")Long skuId,
                           @RequestParam(value = "count",defaultValue = "1")Integer count){
        cartService.add2Cart(skuId,count);
        //System.out.println("拦截器共享的数据："+LoginInterceptor.userInfo());
        //添加商品到购物车中
//        System.out.println(skuId + " , " + count);
        //显示添加成功的页面:
        //-- 查询成功页面需要的添加成功的商品数据 转发到添加成功的页面  会导致表单重复提交
        return "redirect:http://cart.gmall.com/addCart.html?skuId="+skuId;
    }

    //跳转到添加成功页面的接口：
    @GetMapping("addCart.html")
    public String addCartHtml(@RequestParam("skuId")String skuId,
                              Model model){
        //userId/UserKey  + skuId
        CartInfo cartInfo = cartService.queryCartInfoBySkuId(skuId);
        model.addAttribute("cart",cartInfo);
        return "addCart";
    }




    @ResponseBody
    @GetMapping("test")
    public String testExecution() throws ExecutionException, InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("开始执行同步代码...");
        //ListenableFuture: 可以阻塞获取异步任务结果的对象
        String r1 = cartService.execution1();//3
        String r2 = cartService.execution2();//5
        /*
            异常处理：
                出现异常后要么按照我们自己的逻辑返回错误，要么是希望代码正常执行结束

            异步任务执行时:已经配置了失败成功的回调，出现异常时，异常回调已经处理了异常，
                            无需抛出异常终止程序执行

         */
        System.out.println("exe1-结果："+r1 + " ,exe2-结果： "+r2);
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        return "success";
    }

}
